enyo.kind({
	name: "webFTP.HostList",
	kind: enyo.SlidingView,
	
	dragAnywhere: false, 
	fixedWidth: true,
	events: {
		onConfig: ""
	},
	components: [
		{
		    name: "db",
		    kind: "onecrayon.Database",
		    database: 'ext:HostsDatabase',
		    version: "2",
		    debug: false
		},
		{kind: "Header", components: [
			{content:"Hosts"},
			{kind: "Spacer"},
			{kind: "Spinner", name: "progress", style: "top: 0px; height: 30px;", repeat: 20}
		]},
		{kind: "Scroller", flex: 1, components: [
			{kind: "VirtualList", name: "hostList", onSetupRow: "setupRow", components: [ 
				{kind: "SwipeableItem", name:"hostInfo", tapHighlight: false, confirmCaption: "Delete", onclick: "itemClick", 
					className: "host-item-full",
					onConfirm: "swipeDelete", 
					components: [
						{kind: "HFlexBox", components: [
							{name: "listItemHostType"},
						    {name: "listItemID"},
							{kind: "VFlexBox", flex: 1, components: [
								{name: "listItemTitle", flex: 1, className: "host-tit"},
								{name: "listItemHost", className: "host-host"}
							]},
							{kind: "ToolButton", 
								icon: "images/config-icon-new.png", 
								align: "right", pack: "center", 
								onclick: "configClick"}
						]},
					]
				}
			]}
		]},
		{kind: "Toolbar", components: [
            {kind: "GrabButton"},
            {kind: "Spacer", flex:1},
            {name: "addHost", icon: "images/menu-icon-new.png", onclick: "addClick"}
        ]}
	],
	create: function() {
		this.inherited(arguments);

		this.$.progress.show();
		
		this.$.db.setSchemaFromURL('schemas/schema.json', {
		    onSuccess: function() {
				this.finishFirstRun();
			}.bind(this)
		});
		
		this.ResultsData = [];
		this.lastInsertId = 0;		
	},
	getHostListItem: function(inHost) {
		var host = this.ResultsData[inHost]; 

		return host;
	},
	addClick: function(inSender, inEvent) {
		this.insertItem();
	},
	insertItem: function() {
		var myapp = this;
		
		var data_sql = this.$.db.getInsert("Hosts", {
			"host"	:	null,
			"title"	:	null
		});
		
		this.$.db.query(data_sql,{
			onSuccess: function(data) {
				this.finishFirstRun();
			}.bind(this)
			,
			onError: function() {
				myapp.warn("Error Trying to Insert Data " + data_sql);
				this.finishFirstRun();
			}
		});
	},
	editHost: function(inHost) {
		this.warn("editHost Fired");
		var myapp = this;
		
		var data_sql = this.$.db.getUpdate("Hosts", {
			"title" : 	inHost.title,
    		"host"	:	inHost.host,
    		"port"	:	inHost.port,
    		"type"	:	inHost.type,
    		"user"	:	inHost.user,
    		"pass"	:	inHost.pass,
    		"locDir":	inHost.locDir,
    		"remDir":	inHost.remDir}, {"rowID" : 	inHost.rowID,}
    	);
		
		this.$.db.query(data_sql,{
			onSuccess: function(data) {
				this.finishFirstRun();
			}.bind(this)
			,
			onError: function() {
				myapp.warn("Error Trying to Insert Data " + data_sql);
				this.finishFirstRun();
			}
		});
	},
	swipeDelete: function(inSender, inIndex) {
		var recToDelete = this.ResultsData[inIndex];
		
		var data_sql = this.$.db.getDelete('Hosts', {
			"rowID" : recToDelete.rowID
		});
		
		var myapp = this;
		this.$.db.query(data_sql, {
			onSuccess: function(data) {
				myapp.ResultsData = data;  /* HERE I SAVE THE RESULTS */
				this.finishFirstRun();
			}.bind(this)
		});
	},
	setupRow: function(inSender, inIndex) {
		var row = this.ResultsData[inIndex];    /* THIS.RESULTSDATA IS UNDEFINED */
		
		if (row) {
			this.$.listItemID.setContent(row.rowID);
			this.$.listItemID.hide();
			this.$.listItemTitle.setContent(row.title);
			this.$.listItemHost.setContent(row.host);
			
			if ((row.title == null || row.title == "") && 
					(row.host == null || row.host == "")) {
				this.$.hostInfo.setStyle("background-image: url(images/insertHostData.png); background-position: left; " +
										 "background-repeat: no-repeat;");
			} else {
				this.$.hostInfo.setStyle("background-image: none;");
			}
			
			if (row.rowID > this.lastInsertId) {
				this.lastInsertId = row.rowID;
			}
			
			return true;
		}
		
	},
	finishFirstRun: function() {
		this.$.progress.show();
		
		var data_sql = this.$.db.getSelect("Hosts", ['rowID', 'title', 'host', 'port', 'type', 'user', 'pass', 'locDir', 'remDir']);
		
		var myapp = this;
		this.$.db.query(data_sql, {
			onSuccess: function(data) {
				myapp.ResultsData = data;  /* HERE I SAVE THE RESULTS */
				if (data.length > 0) {
					this.$.hostList.setStyle("background-image: none;");
				} else {
					this.warn("false");
					this.$.hostList.setStyle("background-image: url(images/addNewHost.png); background-position: right bottom; " +
											 "background-repeat: no-repeat;");
				}
				
				this.$.progress.hide();
				this.$.hostList.refresh();
			}.bind(this)
		});
    },
    configClick: function(inSender, inEvent) {
    	this.doConfig(inEvent.rowIndex);
    },
});